import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.derby.jdbc.EmbeddedSimpleDataSource;

//WE need to shutdown the database correctly
//Also we need to be able to change the derby DB PATH variable... - It would also be nice to have profiles and be able to load profiles...


public class client {
	
	static boolean checksum = false;
       
        public static ResultSet query(String query, String DB) throws SQLException{
               
        	 	
                EmbeddedSimpleDataSource ds = new EmbeddedSimpleDataSource();

                //Setup the connection to the database
                String dbName = DB;
                ds.setDatabaseName(dbName);
               
                //DB Vars
                Connection conn = null;
                Statement s = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
               
                //Gets the connection going       
                try{
                		conn = ds.getConnection();
                        conn = DriverManager.getConnection("jdbc:derby:testDB");
                        if(checksum == false){System.out.println("The database already exists, not creating it..."); checksum = true;}
                        
                }
                catch(SQLException e){
                        System.out.println("database not found creating it now...");
                        if(checksum == false){DriverManager.getConnection("jdbc:derby:testDB;create=true");}
                        checksum = true;
                }
               
               
                try{
                		s = conn.createStatement();
                                String delims = "[ ]+";
                                String[] tokens = query.split(delims);
                               
                                if(tokens[0].equalsIgnoreCase("UPDATE" ) || tokens[0].equalsIgnoreCase("INSERT") || tokens[0].equalsIgnoreCase("DROP")
                                                || tokens[0].equalsIgnoreCase("CREATE")){
                                        s.execute(query);
                                        System.out.println("OK");
                                }
                                else{
                                rs = s.executeQuery(query);
                                while (rs.next()){
                                    System.out.println(rs.getString(1));
                                    }
                                }
                       
                        }
               
                catch (SQLException e){
                        System.out.println("Oppps error...");
                        e.printStackTrace();
                }
                
                return rs;
                        
        }
       
        public static void main(String[] args) throws SQLException, IOException{
               
                //Global Vars
                String textLine = "";
                ResultSet rs = null;
                String DB = null;
                BufferedReader lineOfText = new BufferedReader(new InputStreamReader(System.in));
               
                System.out.println("When you are done please just hit 'quit' (no quotes) and enter!");
                System.out.print("Please enter your DB Name: ");
                DB = lineOfText.readLine();
                while( ! textLine.equalsIgnoreCase("quit" ) ){

                    try {
                        System.out.print("Please Enter your Query: ");
                                textLine = lineOfText.readLine();
                                if(textLine.equalsIgnoreCase("quit")){break;}else{
                                	query(textLine, DB);
                                }
                        } catch (IOException e) {
                                System.out.println("Sorry but the data you entered is not support try again... Error[1] Trace:");
                                e.printStackTrace();
                        }      
                       
                       
                       
                }      
        }

}

